/**
 * Created by wxy on 2018/1/10.
 */
;(function () {
    angular.module('user', [
        'answer'
    ])
        .service('UserService', [
            '$state',
            '$http',
            function ($state, $http) {
                var me = this;
                me.signup_data = {};
                me.login_data = {};
                me.data = {};

                me.read = function (param) {
                    return $http.post('/laravel54/public/index.php/api/user/read', param)
                        .then(function (r) {
                            if (r.data.status) {
                                me.current_user = r.data.data;
                                me.data[param.id] = r.data.data;

                            }
                            else {
                                if (r.data.status == 0)
                                    $state.go('login');
                            }
                        }, function (e) {

                        })
                };
                me.signup = function () {
                    $http.post('/laravel54/public/index.php/api/signup', me.signup_data)
                        .then(function (r) {
                            console.log('status', r.data.status);
                            if (r.data.status == 200) {
                                me.signup_data = {};
                                $state.go('login');
                            }
                        }, function (e) {

                        })
                };
                me.login = function () {
                    $http.post('/laravel54/public/index.php/api/login', me.login_data)
                        .then(function (r) {
                            console.log('status', r.data.status);
                            if (r.data.status == 200) {
                                me.login_data = {};
                                //$state.go('home');
                                location.href = 'http://localhost/laravel54/public/index.php';
                            } else {
                                me.login_failed = true;
                            }
                        }, function (e) {
                            console.log('e', e);
                        })
                };
                me.username_exists = function () {
                    $http.post('/laravel54/public/index.php/api/user/exist', {username: me.signup_data.username})
                        .then(function (r) {
                            console.log('status', r.data.status);
                            if (r.data.status == 200)
                                me.signup_username_exists = false;
                            else
                                me.signup_username_exists = true;
                        }, function (e) {
                            console.log('e', e);
                        })
                }
            }])
        .controller('SignupController', [
            '$scope',
            'UserService',
            function ($scope, UserService) {
                $scope.User = UserService;
                $scope.$watch(function () {

                    return UserService.signup_data;
                }, function (n, o) {
                    //todo
                    if ((n.username != o.username) && (n.username != '')) {
                        UserService.username_exists();
                    }
                }, true)
            }
        ])
        //登录
        .controller('LoginController', [
            '$scope',
            'UserService',
            function ($scope, UserService) {
                $scope.User = UserService;
            }
        ])

        .controller('UserController', [
            '$scope',
            '$stateParams',
            'UserService',
            'AnswerService',
            'QuestionService',
            function ($scope, $stateParams, UserService, AnswerService, QuestionService) {
                $scope.User = UserService;

                UserService.read($stateParams)

                AnswerService.read({user_id: $stateParams.id})
                    .then(function (r) {
                        if (r)
                            UserService.his_answers = r;
                        console.log('$stateParams', r);
                    });


                QuestionService.read({user_id: $stateParams.id})
                    .then(function (r) {
                        if (r)
                            UserService.his_questions = r;
                        console.log('$stateParams', r);
                    })
            }
        ])
})();